package com.system.service.impl;

import com.system.mapper.BuyerMapper;
import com.system.pojo.PlanLineDto;
import com.system.pojo.SalePlanDto;
import com.system.pojo.TS_ITEM_DOC;
import com.system.pojo.buyerDoC;
import com.system.service.BuyerService;
import com.system.util.ExceptionUtil;
import com.system.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BuyerServiceImpl implements BuyerService {

    @Autowired
    private BuyerMapper buyerMapper;

    /**
     * 获取存货列表
     *
     * @return
     */
    @Override
    public List<TS_ITEM_DOC> getItemList() {
        return buyerMapper.getItemList();
    }

    @Override
    public List<buyerDoC> getVenderList() {
        return buyerMapper.getVenderList();
    }

    @Override
    @Transactional
    public void addSalePlan(SalePlanDto salePlanDto) {
        salePlanDto.setPlanOrderNum(StringUtil.getOrderIdByUUId());

        if (null == salePlanDto.getIsFollow()) {
            salePlanDto.setIsFollow("0");
        } else {
            salePlanDto.setIsFollow("1");
        }
        String[] invo = salePlanDto.getInventoryName().split(",");
        PlanLineDto planLineDto = new PlanLineDto();
        planLineDto.setPlanOrderNum(salePlanDto.getPlanOrderNum());
        planLineDto.setInventoryName(invo[0]);
        planLineDto.setInventoryCode(invo[1]);
        planLineDto.setNum(salePlanDto.getPlanNum());
        //表体
        int r1 = buyerMapper.addPlanLine(planLineDto);
        //表头
        int r2 = buyerMapper.addPlanHead(salePlanDto);
        if (r1 == 0 || r2 == 0) {
            throw new ExceptionUtil("添加失败！");
        }
    }
}
